// ButtonController.ts
import DiceController from './DiceController'
const { ccclass, property } = cc._decorator;

@ccclass
export default class ButtonController extends cc.Component {
    @property(cc.Node)
    diceNode: cc.Node = null; // 关联骰子节点

    @property(cc.Node)
    rollButton: cc.Node = null;  // 引用按钮节点

    onLoad() {
        // 监听按钮点击事件
        //this.node.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        this.rollButton.on(cc.Node.EventType.TOUCH_END, this.onClick, this);
        // touch input
        this.rollButton.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        //this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        //this.node.parent.on(cc.Node.EventType.TOUCH_END, this.onTouchEnded, this);
    }

    onTouchEnd() {
        console.log("---onTouchStart---")
        this.onClick()
    }

    onClick() {
        console.log("---onClick---")
        // 获取 DiceController 脚本组件并调用 rollDice 方法
        //const diceScript = this.diceNode.getComponent('DiceController') as DiceController;
        var dice = this.diceNode;
        console.log("---dice---",dice)
        const diceScript = dice.getComponent('DiceController') as DiceController;
        if (diceScript) {
            diceScript.rollDice();
        } else {
            console.error("DiceController not found on the target node.");
        }
    }
}

